package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObj;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @GetMapping("/headlines")
    public Object getHeadline(StrategyCommentQueryObj qo){
        qo.setPageSize(50);
        PageInfo headlines = strategyCommentService.getHeadline(qo);
        return headlines;
    }
    @GetMapping("/{strategyCommentId}/details")
    public Object getStrategyCommentDetailsById(Long strategyCommentId){
        StrategyComment strategyComment = strategyCommentService.getStrategyCommentDetailsById(strategyCommentId);
        return strategyComment;
    }
    @GetMapping("/{strategyId}/comments")
    public Object getStrategyCommentByStrategyId(@ModelAttribute("qo")StrategyCommentQueryObj qo){
        qo.setOrderBy("createTime desc");
        PageInfo pageInfo = strategyCommentService.getStrategyCommentByStrategyId(qo);
        return pageInfo;
    }

    //获取该用户的所有的评论
    @GetMapping("/{userId}")
    public PageInfo listStrategy(StrategyCommentQueryObj qo){
        //通过他的评论时间排序
        qo.setOrderBy("commendTime desc");
        return strategyCommentService.selectByUserId(qo);
    }
    //添加评论

    /**
     *
     * @myTags 之定义的标签
     * @param tag 标签
     * @param strategyComment
     * @return
     */
    @PostMapping
    public JSONResult save(Long[] tag,String myTags,StrategyComment strategyComment){
        User user = UserContext.getCurrentUser();
        strategyComment.setUser(user);
        strategyCommentService.save(tag,myTags,strategyComment);
        return new JSONResult();
    }
}

